package com.zkthink.ceres.product.controller;

import com.zkthink.ceres.product.dto.*;
import com.zkthink.ceres.product.entity.ProductCategory;
import com.zkthink.ceres.product.service.ProductCategoryService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.zkthink.base.controller.SuperController;
import com.zkthink.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zkthink.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 商品分类表
 * </p>
 *
 * @author JustArgo
 * @date 2020-05-03
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/productCategory")
@Api(value = "ProductCategory", tags = "商品分类表")
//@PreAuth(replace = "productCategory:")
public class ProductCategoryController /*extends SuperController<ProductCategoryService, Long, ProductCategory, ProductCategoryPageDTO, ProductCategorySaveDTO, ProductCategoryUpdateDTO> */{

    @Autowired
    private ProductCategoryService productCategoryService;

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    /*@Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<ProductCategory> productCategoryList = list.stream().map((map) -> {
            ProductCategory productCategory = ProductCategory.builder().build();
            //TODO 请在这里完成转换
            return productCategory;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(productCategoryList));
    }*/

    /** 新增类目
     * @param dto
     * @return
     */
    @ApiOperation(value = "新增类目")
    @PostMapping("/addCategory")
    public R<Boolean> addCategory(@RequestBody List<ProductCategoryDTO> dtoList){
        return productCategoryService.addCategory(dtoList);
    }

    /** 修改类目
     * @param dto
     * @return
     */
    @ApiOperation(value = "修改类目")
    @PostMapping("/updateCategory")
    public R<Boolean> updateCategory(@RequestBody List<ProductCategoryDTO> dtoList){
        return productCategoryService.updateCategory(dtoList);
    }

    /** 逻辑删除类目
     * @param dto
     * @return
     */
    @ApiOperation(value = "逻辑删除类目")
    @PostMapping("/delCategory")
    public R<Boolean> delCategory(@RequestBody ProductCategoryUpdateDTO dto){
        log.info("id:{}, depth:{}",dto.getId(),dto.getDepth());
        return productCategoryService.delCategory(dto.getId(),dto.getDepth());
    }

    /** 查询所有类目
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询所有类目")
    @PostMapping("/queryAllCategory")
    public R<List<ProductCategoryDTO>> queryAllCategory(){
        return productCategoryService.queryAllCategory();
    }

    /** 查询所有类目
     * @param dto
     * @return
     */
    @ApiOperation(value = "分页查询类目，先查询出一级，再递归查询子类目")
    @PostMapping("/findPageCategory")
    public R<AdminProductCategoryPageDTO> findPageCategory(@RequestBody PageQueryDTO pageQueryDTO){
        return productCategoryService.findPageCategory(pageQueryDTO);
    }

    /** 查询单个类目，返回层级类目
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询单个类目")
    @PostMapping("/queryOneCategory")
    public R<ProductCategoryDTO> queryOneCategory(@RequestBody IdQueryDTO dto){
        return productCategoryService.queryOneCategory(dto);
    }

    /** 根据父类目id,查询子级类目列表
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询子级类目")
    @PostMapping("/queryChildCategory")
    public R<List<ProductCategoryDTO>> queryChildCategory(@RequestBody IdQueryDTO dto){
        return productCategoryService.queryChildCategory(dto, false);
    }

    /** 根据层级数，查询对应的类目
     * @param depth
     * @return
     */
    @ApiOperation(value = "查询N级类目")
    @GetMapping("/findCategoryListByDepth/{depth}")
    public R<List<ProductCategoryDTO>> findCategoryListByDepth(@PathVariable Integer depth){
        return productCategoryService.findCategoryListByDepth(depth, false);
    }
}
